//
//  RootViewController.m
//  瓜子电台
//
//  Created by lanou on 16/8/5.
//  Copyright © 2016年 YueZhang. All rights reserved.
//

#import "RootViewController.h"
#import "SubscriptionViewController.h"
#import "DiscoverViewController.h"
#import "UserViewController.h"
#import "DownloadViewController.h"
#import "MusicPlayerViewController.h"
#import "MusicPlayerViewController.h"

@interface RootViewController ()

@property (nonatomic, strong) UIImageView *backPic;

@property (nonatomic, strong) UIButton *btn;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DiscoverViewController *disVC = [[DiscoverViewController alloc] init];
    SubscriptionViewController *subVC = [[SubscriptionViewController alloc] init];
    UserViewController *userVC = [[UserViewController alloc] init];
    DownloadViewController *downloadVC = [[DownloadViewController alloc] init];
    MusicPlayerViewController *musicPlayerVC = [[MusicPlayerViewController alloc] init];
    [self createTabBarWithViewController:disVC name:@"发现" Title:@"发现" image:[UIImage imageNamed: @"发现"]];
    [self createTabBarWithViewController:subVC name:@"订阅听" Title:@"订阅听" image:[UIImage imageNamed:@"订阅"]];
    [self createTabBarWithViewController:musicPlayerVC name:nil Title:nil image:nil];
    [self createTabBarWithViewController:downloadVC name:@"下载听" Title:@"下载听" image:[UIImage imageNamed:@"下载"]];
    [self createTabBarWithViewController:userVC name:@"我的" Title:@"我的" image:[UIImage imageNamed:@"我的"]];
    
    //覆盖原生Tabbar的上横线
    [[UITabBar appearance] setShadowImage:[self createImageWithColor:[UIColor clearColor]]];
    [[UITabBar appearance] setBackgroundImage:[self createImageWithColor:[UIColor clearColor]]];
    
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, -16, self.tabBar.frame.size.width, self.tabBar.frame.size.height + 20)];
    imgV.image = [UIImage imageNamed:@"tabbar_bg@3x"];
    [self.tabBar insertSubview:imgV atIndex:0];
    
    self.backPic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.tabBar.frame.size.height, self.tabBar.frame.size.height)];
    _backPic.backgroundColor = [UIColor redColor];
    [_backPic.layer setMasksToBounds:YES];
    [_backPic.layer setCornerRadius:self.tabBar.frame.size.height / 2];
    _backPic.center = CGPointMake(self.tabBar.centerX, self.tabBar.centerY - 8);
    
    self.btn = [UIButton buttonWithType:normal];
    _btn.frame = CGRectMake(0, 0, self.tabBar.frame.size.height / 2, self.tabBar.frame.size.height / 2);

    self.btn.center = CGPointMake(self.backPic.ownWidth / 2 + 2, self.backPic.ownHeight / 2);
    [self.btn setBackgroundImage:[UIImage imageNamed:@"播放"] forState:(UIControlStateNormal)];
    [self.btn addTarget:self action:@selector(playAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.backPic.userInteractionEnabled = YES;
    [self.backPic addSubview:self.btn];
    [self.view addSubview:self.backPic];
}

- (void)playAction {
    MusicPlayerViewController *vc = [[MusicPlayerViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)createTabBarWithViewController:(UIViewController *)vc name:(NSString *)name Title:(NSString *)title image:(UIImage *)image
{
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:vc];
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:11]};
    [[UITabBarItem appearance] setTitleTextAttributes:dic forState:(UIControlStateNormal)];
    [vc.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x"] forBarMetrics:0];
    vc.title = name;
    naVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:title image:image tag:100];
    [self addChildViewController:naVC];
    self.tabBar.tintColor = [UIColor orangeColor];
    
    
}

#pragma mark ----- 覆盖线条方法 -----
-(UIImage *)createImageWithColor:(UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
